package com.zhongxi.guoyuan.adapter.coinPost;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.coinPost.CoinPostGoodsData;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.MyTextUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/6/28.
 */

public class CoinEvaluateAdapter extends BaseAdapter {

    private List<CoinPostGoodsData> data;
    private Context context;
    private Handler handler;

    private int score;
    private Message msg;

    public CoinEvaluateAdapter(Context context, List<CoinPostGoodsData> listGoods, Handler handler) {
        this.context = context;
        this.data = listGoods;
        this.handler = handler;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try {
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.coin__post_evaluate, parent, false);
                holder.image = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.name = (TextView) convertView.findViewById(R.id.tvName);
                holder.attr = (TextView) convertView.findViewById(R.id.tvAttr);
                holder.price = (TextView) convertView.findViewById(R.id.tvPrice);
                holder.number = (TextView) convertView.findViewById(R.id.tvNum);
                holder.pricetext = (TextView) convertView.findViewById(R.id.tv_post_evaluate);

                holder.ratingBar = (RatingBar) convertView.findViewById(R.id.post_evaluate_ratingbar);
                holder.content = (EditText) convertView.findViewById(R.id.post_evaluate_content);
                holder.score = (TextView) convertView.findViewById(R.id.post_evaluate_score);
                holder.textNum = (TextView) convertView.findViewById(R.id.post_evaluate_textNum);
                holder.submit = (TextView) convertView.findViewById(R.id.post_evaluate_submit);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (null != data) {
                CoinPostGoodsData coinPostGoodsData = data.get(position);
                Glide.with(context).load(Url.IMG_URL + coinPostGoodsData.getThumbnail()).placeholder(R.mipmap.iv_eg).fitCenter().into(holder.image);
                holder.name.setText(coinPostGoodsData.getGoodsname());
                holder.price.setText(coinPostGoodsData.getGoodsunitprice() + "元");
                holder.number.setText("X" + coinPostGoodsData.getGoodsnum());

                JSONObject obj = new JSONObject(coinPostGoodsData.getGattr());
                Iterator<String> iterator = obj.keys();
                String str = "";
                while (iterator.hasNext()) {
                    str += "/" + obj.getString(iterator.next());
                }
                holder.attr.setText(str);
                holder.pricetext.setVisibility(View.GONE);
                final ViewHolder finalHolder = holder;
                holder.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                    @Override
                    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                        score = (int) finalHolder.ratingBar.getRating();
                        finalHolder.score.setText(score + "分");
                    }
                });
                score = (int) holder.ratingBar.getRating();

                holder.content.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

                    @Override
                    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

                    @Override
                    public void afterTextChanged(Editable editable) {
                        MyTextUtils.TextChangeUtils(context, finalHolder.textNum, editable, 200);
                    }
                });
                if (coinPostGoodsData.getStatus()==1){
                    holder.content.setText("");
                }
                holder.submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String trim = finalHolder.content.getText().toString().trim();
                        msg = Message.obtain();
                        msg.arg1 = position;
                        msg.arg2 = score;
                        msg.what = 0;
                        msg.obj = trim;
                        handler.sendMessage(msg);
                        finalHolder.content.setHint("对此服务我要说……");
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView image;
        private RatingBar ratingBar;
        private EditText content;
        private TextView name, price, attr, number, score, textNum, submit, pricetext;
    }

}
